package com.alibaba.citrus.cr.b2b.trade.order.repository.impl;
import javax.annotation.Resource;

import com.alibaba.citrus.cr.b2b.trade.order.repository.DeliverOrderQueryRepository;

import com.epoch.app.crb2btradecenter.domain.deliveryorder.dto.LoadDeliveryOrderRequest;
import com.epoch.app.crb2btradecenter.domain.deliveryorder.model.DeliveryOrder;
import com.epoch.app.crb2btradecenter.domain.deliveryorder.service.DeliveryOrderService;
import com.epoch.app.crb2btradecenter.sdo.DeliverOrderSDO;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

@Component
public class DeliverOrderQueryRepositoryImpl implements DeliverOrderQueryRepository {

    @Resource
    private DeliveryOrderService deliveryOrderService;

    @Override
    public DeliverOrderSDO query(String id) {
        LoadDeliveryOrderRequest loadDeliveryOrderRequest = new LoadDeliveryOrderRequest();
        if (!StringUtils.isEmpty(id)) {
            loadDeliveryOrderRequest.setId(id);
        }
        DeliveryOrder deliveryOrder = deliveryOrderService.loadDeliveryOrder(loadDeliveryOrderRequest);
        return deliveryOrderSDOConverter(deliveryOrder);
    }

    private DeliverOrderSDO deliveryOrderSDOConverter(DeliveryOrder deliveryOrder) {
        DeliverOrderSDO deliverOrderSDO = new DeliverOrderSDO();
        deliverOrderSDO.setId(deliveryOrder.getId());
        deliverOrderSDO.setOrderId(deliveryOrder.getOrderId());
        deliverOrderSDO.setTenantId(deliveryOrder.getTenantId());
        deliverOrderSDO.setCustomerCode(deliveryOrder.getCustomerCode());
        deliverOrderSDO.setDeliveryPartCode(deliveryOrder.getDeliveryAddressCode());
        deliverOrderSDO.setWarehouseCode(deliveryOrder.getWarehouseCode());
        deliverOrderSDO.setWarehouseName(deliveryOrder.getWarehouseName());
        deliverOrderSDO.setDeliveryTime(deliveryOrder.getDeliveryTime());
        deliverOrderSDO.setReceiveTime(deliveryOrder.getReceivedTime());
        if (deliveryOrder.getDeliveryStatus() != null) {
            deliverOrderSDO.setDeliveryStatus(Integer.parseInt(deliveryOrder.getDeliveryStatus()));
        }
        deliverOrderSDO.setOutDeliveryId(deliveryOrder.getOutDeliveryOrderId());
        deliverOrderSDO.setTransportModeCode(deliveryOrder.getTransportModeCode());
        deliverOrderSDO.setTransportModeName(deliveryOrder.getTransportModeName());
        deliverOrderSDO.setSyncVersion(deliveryOrder.getSyncVersion());
        deliverOrderSDO.setGmtCreate(deliveryOrder.getGmtCreate());
        deliverOrderSDO.setGmtModified(deliveryOrder.getGmtModified());
        deliverOrderSDO.setCreator(deliveryOrder.getCreator());
        deliverOrderSDO.setUpdater(deliveryOrder.getUpdater());
        deliverOrderSDO.setContact(deliveryOrder.getContact());
        deliverOrderSDO.setContactPhone(deliveryOrder.getContactPhone());
        deliverOrderSDO.setPostcode(deliveryOrder.getPostCode());
        return deliverOrderSDO;
    }

}
